/**
 * resource management 
 */
var _resourceId;
var resource = {
		/**
		 * Show resource tree
		 */		
		init : function(){
			var resourceTree = baseURL + "/system/resource/parentList"
			$.ajax({
				type : "get",
				url : resourceTree,
				dataType : "json",
				success : function(json){
					$('#resourceTree').tree({
						data : [json],
						animate : true,
						checkbox : true,
						cascadeCheck : false,
						onClick : function(node){
							var resourceId=node.id;
							resource.edit(resourceId);
						}
					})
				}
			});
		},
/**
	 * Show resource list
	 */
	list : function() {
		// Request resource list url from resourceController.
		var resourceListUrl = baseURL + "/system/resource/list"
		$.ajax({
			type : "get",
			url : resourceListUrl,
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#list").empty().html(html);
			}
		});
	},
	
		
	/**
	 * New/Edit resource
	 */
	edit : function(value) {
		if (value != "-1"){
			// Get the first resourceId from checked buttons.
			var resourceId = value;
			//var resourceId = $("input[name='id']:checked").eq(0).val();
			_resourceId=resourceId;
			// The title of edit dialog
			var title = "Create resource";
			// If userId is not empty, edit the user info; or new it.
			if (resourceId && resourceId != "") {
				title = "Edit resource";
			}
			
			$.ajax({
				type : "post",
				url : baseURL + "/system/resource/edit",
				data :{resourceId : resourceId},
				dataType : "html",
				success : function(html) {
					// Request successfully and fill the data.
					$("#resourceInfo").empty().html(html);
				}
			});
		}		
	},
	

	
	/**
	 * Save resource
	 */
	save : function() {
		
		
		if($("#editresourceForm").form('validate')==true){
			// Click the "Confirm" button to save user info.
			var info=[];
			$('[name="res"]').each(function(){
				info.push($(this).val()); 
	            });  
			
			var status=$('input[name="statusSelect"]:checked').val();
			var dangerLevel = $('input[name="dangerLevelSelect"]:checked').val();
			var parentId=$('#tree').combotree('getValue');
			var resourceId=_resourceId;
			$.ajax({
				type : "post",
				url : baseURL + "/system/resource/save",
				data : {info : info , status : status , resourceId : resourceId , parentId : parentId, dangerLevel : dangerLevel},
				traditional: true,
				dataType : "json",
				success : function(json) {
					if (json.result && json.result == "success") {
						common.showTip({
							success : true,
							title : localeMsg.common.info,
							text : localeMsg.common.saveSuccess,
							callback : function() {
								resource.list();
							}
						});
					}else {
						common.showTip({
							title : localeMsg.common.warn,
							text : localeMsg.common.saveFailed,
						});
					}
				}
			});
		}
		else{
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.invalidData
			});
		}
		
		

		
	},
	
	cancel : function(){
		$("#resourceInfo").empty();
	},
	
	remove : function(){
		var nodes=$('#resourceTree').tree('getChecked');
		if (Object.keys(nodes).length === 0){
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}
		else{
			var confirmMessage = localeMsg.common.relateddeleteconfirm;
				common.confirmDialog(confirmMessage,resource.removeDel);
		}

	},
	
	/**
	 * Remove resources
	 */
	removeDel : function() {
		//  Remove  records
		var resourceId=[];

		var nodes=$('#resourceTree').tree('getChecked');
		for(var x in nodes){
			resourceId.push(nodes[x].id);
		}
		$.ajax({
			type : "post",
			url : baseURL + "/system/resource/remove",
			data : {resourceId : resourceId},
			traditional: true,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						success : true,
						title : localeMsg.common.info,
						text : localeMsg.common.deleteSuccess,
						callback : function() {
							resource.list();
						}
					});
				} else {
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.common.deleteFailed,
					});
				}
			}
		});
	},
	

	/**
	 *表单数据验证
	*/
	
	validate : function(){
		
	    $('#input_name').validatebox({
	        required: true,
	        validType: ['minLength[1]','name']
	    });
	    
	    $('#input_nameEn').validatebox({
	        required: true,
	        validType:  ['minLength[1]','name']
	    });
	    
	    $('#input_sort').validatebox({
	        required: true,
	        validType: ['minLength[1]','maxLength[3]','digits']
	    });
	    
	    $('#input_match_Sort').validatebox({
	        required: true,
	        validType: ['minLength[1]','maxLength[2]','digits']
	    });
	    
	    $('#input_openTarget').validatebox({
	        required: true,
	        validType: ['maxLength[1]','digits']
	    });
	},
		
	gettreedata : function(){
		var oldParentId = $("#parentId").val();
		var parentList = baseURL + "/system/resource/parentList"
		$.ajax({
			type : "get",
			url : parentList,
			dataType : "json",
			success : function(json) {
				// Request successfully and fill the data.
				$("#tree").combotree({
					data : [json],
					lines : true,
					animate : true 
				});
				if (oldParentId==""){
					$("#tree").combotree('setValue', -1);
				}else{
					$("#tree").combotree('setValue', oldParentId);
				}
			}
		});
	},
};
